package com.xinghai.service.impl;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xinghai.domain.JkqActivity;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.mapper.JkqActivityAddressMapper;
import com.xinghai.domain.JkqActivityAddress;
import com.xinghai.service.IJkqActivityAddressService;

/**
 * 活动地址管理Service业务层处理
 *
 * @author xinghai
 * @date 2025-08-02
 */
@Service
public class JkqActivityAddressServiceImpl extends MPJBaseServiceImpl<JkqActivityAddressMapper, JkqActivityAddress>
        implements IJkqActivityAddressService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询活动地址管理列表
     *
     * @param jkqActivityAddress 活动地址管理
     * @return 活动地址管理集合
     */
    @Override
    public List<JkqActivityAddress> list(JkqActivityAddress jkqActivityAddress) {
        MPJLambdaWrapper<JkqActivityAddress> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(JkqActivityAddress.class)
                .setAlias("ad")
                .selectAs(JkqActivity::getTitle,JkqActivityAddress::getActivityTitle)
                .selectAs(JkqActivity::getImageUrl,JkqActivityAddress::getActivityImageUrl)
                .selectAs(JkqActivity::getType,JkqActivityAddress::getActivityType)
                .leftJoin(JkqActivity.class,"a", JkqActivity::getId, JkqActivityAddress::getActivityId);
        if (jkqActivityAddress.getActivityId()!= null ){
            queryWrapper.eq("ad.activity_id", jkqActivityAddress.getActivityId());
        }
        if (jkqActivityAddress.getAddress()!= null  && !jkqActivityAddress.getAddress().isEmpty()){
            queryWrapper.like("ad.address", jkqActivityAddress.getAddress());
        }
        if (jkqActivityAddress.getStatus()!= null  && !jkqActivityAddress.getStatus().isEmpty()){
            queryWrapper.eq("ad.status", jkqActivityAddress.getStatus());
        }
        if (jkqActivityAddress.getActivityType()!= null  && !jkqActivityAddress.getActivityType().isEmpty()){
            queryWrapper.eq("a.type", jkqActivityAddress.getActivityType());
        }
        return this.baseMapper.selectJoinList(JkqActivityAddress.class,queryWrapper);
    }

    /**
     * 导入活动地址管理信息
     *
     * @param list 活动地址管理信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JkqActivityAddress> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JkqActivityAddressMapper jkqActivityAddressMapper = sqlSession.getMapper(JkqActivityAddressMapper.class);
            list.forEach(jkqActivityAddressMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
